$(function () {
    var base = "/";




    function numToSize(num) {
        var numString = num.toString();
        num = (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
        return num;


    }

    function isShow(type) {
        var houseType = $("#houseTypeSelect").val();
        console.log(houseType)

        if (type != 0 && houseType != "户型" && houseType != "") {
            getIsValid();
        } else {
            $("#bondInp").val("保证金额：-万元");
        }
    }

    //获取保证金额
    function getIsValid() {
        var id = $("#houseTypeSelect").val();
        $.ajax({
            url:$.Utils.urlPath("house/getIsValid.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                id: id
            },
            success: function (res) {
                if (res.code == "success") {
                    $("#bondInp").val("保证金额：" + res.content.deposit + "万元");
                }

            }
        })
    }

    ///
    //获取地区

    function getArea(val) {
        $.ajax({
            url:  $.Utils.urlPath("house/getArea.jhtml"),
            type: "POST",
            dataType: "json",
            success: function (res) {
                if (res.code == "success") {
                    var html = "";
                    var obj = res.content;
                    if (obj.length < 1) {
                        html += "<option  disabled selected>地区</option>";
                    } else {
                        if (val == undefined) {
                            html += "<option  disabled selected>地区</option>";
                            for (var i in obj) {
                                html += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
                            }
                        } else {
                            html += "<option  disabled>地区</option>";
                            for (var i in obj) {
                                if (obj[i].id == val) {
                                    html += "<option value='" + obj[i].id + "' selected>" + obj[i].name + "</option>";
                                } else {
                                    html += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
                                }

                            }
                        }

                    }

                    $("#areaSelect").html(html);
                }

            }
        })
    }

    //获取楼盘
    function getEstateList(area, val) {
        var id = area == undefined ? ($("#areaSelect").val()) : area;
        $.ajax({
            url: $.Utils.urlPath( "house/getEstateList.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                area: id
            },
            success: function (res) {
                if (res.code == "success") {
                    var html = "";
                    var obj = res.content;
                    if (obj.length < 1) {
                        html += "<option  disabled selected>楼盘</option>";
                    } else {
                        if (val == undefined) {
                            html += "<option  disabled selected>楼盘</option>";
                            for (var i in obj) {
                                html += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
                            }
                        } else {
                            html += "<option  disabled>楼盘</option>";
                            for (var i in obj) {
                                if (obj[i].id == val) {
                                    html += "<option value='" + obj[i].id + "' selected>" + obj[i].name + "</option>";
                                } else {
                                    html += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
                                }

                            }
                        }

                    }

                    $("#estateSelect").html(html);
                }

            }
        })
    }


    function getHouseType(estate, val) {
        var id = estate == undefined ? ($("#estateSelect").val()) : estate;
        $.ajax({
            url: $.Utils.urlPath( "house/getHouseType.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                id: id,
            },
            success: function (res) {
                if (res.code == "success") {
                    var html = "";
                    var obj = res.content;
                    if (obj.length < 1) {
                        html += "<option  disabled selected>户型</option>";
                    } else {
                        if (val == undefined) {
                            html += "<option  disabled selected>户型</option>";
                            for (var i in obj) {
                                html += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
                            }
                        } else {
                            html += "<option  disabled >户型</option>";
                            for (var i in obj) {
                                if (obj[i].id == val) {
                                    html += "<option value='" + obj[i].id + "' selected>" + obj[i].name + "</option>";
                                } else {
                                    html += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
                                }

                            }
                        }

                    }

                    $("#houseTypeSelect").html(html);
                }

            }
        })
    }

    //提交报名
    function saveReserveInfo() {
        var obj = $(".signUpBox .line");
        var name = obj.eq(0).find(".inp").val();
        var identity = obj.eq(1).find(".inp").val();
        var phone = obj.eq(2).find(".inp").val();
        var account = obj.eq(3).find(".inp").val();
        var houseId = $("#houseTypeSelect").val();
        var one = identity.substring(0, identity.length - 1);
        var two = identity.substring(identity.length - 1, identity.length);
        identity = two == "x" ? (one + "X") : identity;
        $.ajax({
            url: $.Utils.urlPath("house/saveReserveInfo.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                houseId: houseId,
                name: name,
                identity: identity,
                phone: phone,
                account: account
            },
            success: function (res) {
                if (res.code == "success") {
                    $.Utils.showTip("报名成功,请确认您的账户有足够金额办理该项业务");
                    getSearchResult(identity);
                    $(".inp").val("");
                    getArea();
                    $("#estateSelect").html("<option  disabled selected>楼盘</option>");
                    $("#houseTypeSelect").html("<option  disabled selected>户型</option>");
                    $("#bondInp").val("保证金额：-元");
                    $(".popBox").hide();
                    $(".tabList").eq(1).addClass("active").siblings().removeClass("active");
                    $(".tabBox .tabCon").eq(1).addClass("active").siblings().removeClass("active");
                    $(".popBox .btnbg").hide();

                } else {
                    $.Utils.showTip(res.msg);
                }

            }
        })
    }

    //编辑
    function updateReserveInfo() {
        var obj = $(".signUpBox .line");
        var name = obj.eq(0).find(".inp").val();
        var identity = obj.eq(1).find(".inp").val();
        var phone = obj.eq(2).find(".inp").val();
        var account = obj.eq(3).find(".inp").val();
        var houseId = $("#houseTypeSelect").val();
        var one = identity.substring(0, identity.length - 1);
        var two = identity.substring(identity.length - 1, identity.length);
        identity = two == "x" ? (one + "X") : identity;
        var id = $(".editInfoBtn").attr("data-listId");
        $.ajax({
            url: $.Utils.urlPath( "house/updateReserveInfo.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                id: id,
                houseId: houseId,
                name: name,
                identity: identity,
                phone: phone,
                account: account
            },
            success: function (res) {
                if (res.code == "success") {
                    $.Utils.showTip("编辑成功，请确认您的账户有足够金额办理该项业务");
                    getSearchResult(identity);
                    $(".inp").val("");
                    getArea();
                    $("#estateSelect").html("<option  disabled selected>楼盘</option>");
                    $("#houseTypeSelect").html("<option  disabled selected>户型</option>");
                    $("#bondInp").val("保证金额：-元");
                    $(".popBox").hide();
                    $(".tabList").eq(1).addClass("active").siblings().removeClass("active");
                    $(".tabBox .tabCon").eq(1).addClass("active").siblings().removeClass("active");
                    $(".popBox .btnbg").hide();

                } else {
                    $.Utils.showTip(res.msg);
                }

            }
        })
    }

    function getSearchResult(number) {
        $.Utils.loadingInit();
        $.ajax({
            url: $.Utils.urlPath( "house/getReserveInfo.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                number: number
            },
            success: function (res) {
                if (res.code == "success") {
                    $.Utils.loadingRemove();
                    if (res.content.length > 0) {
                        $(".searchResult").show();
                        var html = "";
                        for (var i in res.content) {
                            var name = res.content[i].name;
                            var identity = res.content[i].identity.toString();
                            identity = identity.substring(identity.length - 4, identity.length);
                            var phone = res.content[i].phone.toString();
                            phone = phone.substring(phone.length - 4, phone.length);
                            var account = res.content[i].account.toString();
                            account = account.substring(account.length - 4, account.length);
                            var deposit = res.content[i].deposit;
                            var state = res.content[i].state;

                            state = state == 0 ? "申请" : state == 1 ? "已冻结" : state == 2 ? "已扣划" : state == 3 ? "已失效" : state == 4 ? "已解冻" : state == 5 ? "信息有误" : "余额不足";
                            html += '<tr>';
                            html += '<td colspan="6" style="text-align: left;background:#FFFFBF;padding-left:15px; border-bottom:0 none;position: relative">';
                            html += '<span>' + res.content[i].house.estate.area.name + " — " + res.content[i].house.estate.name + '</span>';
                            html += '<span style="position: absolute; right: 10px;">保证金额：' + deposit + '万元</span>';
                            html += '</td>';
                            html += '</tr>';
                            html += '<tr>';
                            html += '<td>' + name + '</td>';
                            html += '<td>' + identity + '</td>';
                            html += '<td>' + phone + '</td>';
                            html += '<td>' + account + '</td>';
                            html += '<td>' + state + '</td>';
                            if (state == "余额不足") {
                                html += '<td><a data-listId="' + res.content[i].id + '"  data-houseId="' + res.content[i].house.id + '" data-estateId="' + res.content[i].house.estate.id + '" data-areaId="' + res.content[i].house.estate.area.id + '" data-deposit="' + deposit + '" data-name=' + name + ' data-identity="' + res.content[i].identity + '" data-phone="' + res.content[i].phone + '" data-account="' + res.content[i].account + '" class="frozenBtn" href="javascript:void(0)">足额申请</a></td>';

                            } else if (state == "信息有误") {
                                html += '<td><a data-listId="' + res.content[i].id + '"  data-houseId="' + res.content[i].house.id + '" data-estateId="' + res.content[i].house.estate.id + '" data-areaId="' + res.content[i].house.estate.area.id + '" data-deposit="' + deposit + '" data-name=' + name + ' data-identity="' + res.content[i].identity + '" data-phone="' + res.content[i].phone + '" data-account="' + res.content[i].account + '" class="editInfoBtn" href="javascript:void(0)">编辑</a></td>';
                            } else {
                                html += '<td>-</td>';

                            }
                            html += '</tr>';
                        }
                        $(".resultCon tbody").html(html);
                    } else {
                        $.Utils.showTip("查无结果");
                    }
                }

            }
        })
    }

    // 验证手机号
    function isPhoneNo(phone) {
        var pattern = /^1[34578]\d{9}$/;
        return pattern.test(phone);
    }

    // 验证身份证
    function isCardNo(card) {
        var pattern = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        return pattern.test(card);
    }


    function checkIsExist(phone, identity, id, type) {
        $.ajax({
            url: $.Utils.urlPath( "house/checkIsExist.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                estateId: id,
                identity: identity,
                phone: phone

            },
            success: function (res) {
                if (res.code == "success") {
                    if (type == undefined) {
                        $(".popBox").show();
                    } else {
                        updateReserveInfo();
                    }

                } else {
                    $.Utils.showTip(res.msg)
                }

            }
        })
    }

    $(".tabList").click(function () {
        var index = $(this).index();
        $("#signUpBtn").text("立即报名");
        $(this).addClass("active").siblings().removeClass("active");
        $(".tabBox .tabCon").eq(index).addClass("active").siblings().removeClass("active");
    });
    $("#return").click(function () {
        $(".searchResult").hide();
        $("#numberInp").val("");
        $(".editTitleBox").hide();
        $(".newTitleBox").show();

    });
    $("#searchBtn").click(function () {
        var num = $("#numberInp").val();
        var one = num.substring(0, num.length - 1);
        var two = num.substring(num.length - 1, num.length);
        num = two == "x" ? (one + "X") : num;
        if (num == "") {
            $.Utils.showTip("请输入信息查询");
        } else {
            getSearchResult(num);
        }


    });

    $("#areaSelect").change(function () {
        isShow(0);
        getEstateList();
        $("#houseTypeSelect").html("<option disabled selected>户型</option>")

    });
    $("#estateSelect").change(function () {
        //是否显示金额
        isShow(0);
        getHouseType();
    })
    $("#houseTypeSelect").change(function () {
        //是否显示金额
        isShow();

    });

    $("#signUpBtn").click(function () {
        var obj = $(".signUpBox .line");
        var name = obj.eq(0).find(".inp").val();
        var identity = obj.eq(1).find(".inp").val();
        var phone = obj.eq(2).find(".inp").val();
        var account = obj.eq(3).find(".inp").val();
        var id = $("#houseTypeSelect").val();
        var estateId = $("#estateSelect").val();
        var isPhone = isPhoneNo(phone);
        var isCard = isCardNo(identity);
        id =id== "户型"?"":id;
        $(".signUpBox").find(".line").each(function () {
            var index = $(this).index();
            if (index < 5) {
                var inpVal = $(this).find(".inp").val();
                if (inpVal == "") {
                    var msg = $(this).find(".inp").attr("placeholder")
                    $.Utils.showTip(msg + "未输入")
                    return false;
                } else if (index == 1 && isCard == false) {
                    $.Utils.showTip("身份格式不正确");
                    return false;
                }
                else if (index == 2 && isPhone == false) {
                    $.Utils.showTip("手机号格式不正确");
                    return false;
                } else if (index == 3 && account.length != 4) {
                    $.Utils.showTip("银行卡号请输入后四位");
                    return false;
                }
            } else {
                $(".signUpBox").find(".select").each(function () {
                    var selectVal = $(this).val();
                    var msg = $(this).find("option").eq(0).text();
                    selectVal = (selectVal == "地区" || selectVal == "楼盘" || selectVal == "户型") ? "" : selectVal;
                    if (selectVal == "") {
                        $.Utils.showTip(msg + "未选择");
                        return false;
                    }

                })
            }
        });
        var txt = $(this).text();
        if (name != "" && identity != "" && phone != "" && account != "" && id != "" && isPhone == true && isCard == true && account.length == 4) {
            if (txt == "立即报名") {
                checkIsExist(phone, identity, estateId)
            } else {
                checkIsExist(phone, identity, estateId, 0)
            }
        }


    });
    getArea();

    $(".sureBtn").click(function () {
        $(".popBox .btnbg").show();
        saveReserveInfo();
    });

    $(".cancelBtn").click(function () {
        $(".popBox").hide();
        $(".tabList").eq(1).addClass("active").siblings().removeClass("active");
        $(".tabBox .tabCon").eq(1).addClass("active").siblings().removeClass("active");

    });

    //编辑
    $(document).on("click", ".editInfoBtn", function () {
        $(".tabList").eq(0).addClass("active").siblings().removeClass("active");
        $(".tabBox .tabCon").eq(0).addClass("active").siblings().removeClass("active");
        var name = $(this).attr("data-name");
        var identity = $(this).attr("data-identity");
        var phone = $(this).attr("data-phone");
        var account = $(this).attr("data-account");
        var area = $(this).attr("data-areaId");
        var estate = $(this).attr("data-estateId");
        var house = $(this).attr("data-houseId");
        var deposit = $(this).attr("data-deposit");

        var obj = $(".signUpBox .line");
        obj.eq(0).find(".inp").val(name);
        obj.eq(1).find(".inp").val(identity);
        obj.eq(2).find(".inp").val(phone);
        obj.eq(3).find(".inp").val(account);
        $("#bondInp").val("保证金额：" + deposit + "万元");
        getArea(area);
        getEstateList(area, estate);
        getHouseType(estate, house);
        $(".searchResult").hide();
        $(".editTitleBox").show();
        $(".newTitleBox").hide();
        $("#signUpBtn").text("保 存");

    })
    //申请冻结
    $(document).on("click", ".frozenBtn", function () {
        var name = $(this).attr("data-name");
        var identity = $(this).attr("data-identity");
        var phone = $(this).attr("data-phone");
        var account = $(this).attr("data-account");
        var house = $(this).attr("data-houseId");
        var id = $(this).attr("data-listId");
        $.ajax({
            url: $.Utils.urlPath( "house/updateReserveInfo.jhtml"),
            type: "POST",
            dataType: "json",
            data: {
                id: id,
                houseId: house,
                name: name,
                identity: identity,
                phone: phone,
                account: account
            },
            success: function (res) {
                if (res.code == "success") {
                    $.Utils.showTip("申请成功，请确认您的账户有足够金额办理该项业务");
                    getSearchResult(identity);
                    $(".inp").val("");
                    getArea();
                    $("#estateSelect").html("<option  disabled selected>楼盘</option>");
                    $("#houseTypeSelect").html("<option  disabled selected>户型</option>");
                    $("#bondInp").val("保证金额：-元");
                    $(".popBox").hide();
                    $(".tabList").eq(1).addClass("active").siblings().removeClass("active");
                    $(".tabBox .tabCon").eq(1).addClass("active").siblings().removeClass("active");
                    $(".popBox .btnbg").hide();

                } else {
                    $.Utils.showTip(res.msg);
                }

            }
        })


    })
})